// pages/more-store/more-store.js
var {api, request} = require('../../utils/api.js')
import { ASSETS_BASE_URL, UPLOAD_BASE_URL } from '@/utils/config';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    province: '',
    storeList: [],
    provinceList: [],
    cityList: [],
    areaList: [],
    type: 1,
    selectList: [],
    selectIndex: '',
    selectName: '',
    showProvince: false,
    showCity: false,
    showArea: false,
    baseUrl: ASSETS_BASE_URL
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getStore()
    this.selectProvince()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },
  onClose() {
    let value = []
    this.data.province ? value.push(this.data.province) : ""
    this.data.city ? value.push(this.data.city) : ""
    this.data.area ? value.push(this.data.area) : ""
    let obj = {
      pid:this.data.pid,
      cid:this.data.cid,
      aid:this.data.aid,
      p_id:this.data.p_id,
      c_id:this.data.c_id,
      a_id:this.data.a_id
    }
    if(this.data.hide){
      obj.province = this.data.province
      obj.city = this.data.city
      obj.area = this.data.area
    }else{
      obj.value = value
    }
    this.triggerEvent("close",obj)
  },
  select(e){
    const index = e.detail.index
    if(this.data.type==1){
      this.setData({
        p_id:this.data.provinceList[index].code,
        pid:this.data.provinceList[index].id,
        type:2,
        province:this.data.provinceList[index].name,
        city:"",
        area:"",
        showProvince: false
      })
      this.selectCity()
    }else if(this.data.type == 2){
      // let type = this.data.hide ? 2 :3
      this.setData({
        c_id:this.data.cityList[index].code,
        cid:this.data.cityList[index].id,
        type: 2,
        city:this.data.cityList[index].name,
        area:"",
        showCity: false
      })
      if(!this.data.hide){
        this.selectArea()
      }else{
        this.onClose()
      }
    }else{
      this.setData({
        area:this.data.areaList[index].name,
        aid:this.data.areaList[index].id,
        a_id:this.data.areaList[index].code,
        showArea: false
      })
    }
    this.getStore()
  },
  selectProvince(){
    request('province', 'POST').then(res => {
      if (res.data.code == 200) {
        const selectList = []
        res.data.data.forEach(item => {
          console.log(item.name)
          selectList.push(item.name)
        })
        this.setData({
          type:1,
          provinceList:res.data.data,
          selectList:selectList,
          c_id: ''
        })
      }
    })
  },
  choiceCity(){
    if(!this.data.p_id){
      this.alert("请先选择省份！")
      return
    }
    this.selectCity()
  },
  choiceArea(){
    if(!this.data.c_id){
      this.alert("请先选择城市！")
      return
    }
    this.selectArea()
  },
  selectCity(){
    let params={
      pid:this.data.p_id
    }
    request('city','post',params).then(res => {
      if (res.data.code == 200) {
        const selectList = []
        res.data.data.forEach(item => {
          selectList.push(item.name)
        })
        this.setData({
          type:2,
          cityList:res.data.data,
          selectList:selectList
        })
        console.log(this.data.selectList)
      }
    })
  },
  selectArea(){
    let params={
      cid:this.data.c_id
    }
    request('area','post',params).then(res => {
      if (res.data.code == 200) {
        const selectList = []
        res.data.data.forEach(item => {
          console.log(item.name)
          selectList.push(item.name)
        })
        this.setData({
          type:3,
          areaList:res.data.data,
          selectList:selectList
        })
      }
    })
  },
  showPoup(e) {
    console.log(e.currentTarget.dataset.type)
    this.setData({
      type: e.currentTarget.dataset.type
    })
    switch(e.currentTarget.dataset.type) {
      case '1':
        this.selectProvince()
        this.setData({
          showProvince: true
        })
        break
        case '2':
          
        this.selectCity()
        this.setData({
          showCity: true
        })
        break
        case '3':
        this.selectArea()
        this.setData({
          showArea: true
        })
        break
    }
  },
  onCancel() {
    this.setData({
      showProvince: false,
      showCity: false,
      showArea: false
    })
  },
  getStore() {
    request('/store_lst_page', 'POST', {
      p_id: this.data.pid,
      c_id: this.data.cid,
      a_id: this.data.aid
    }).then(res => {
      console.log(res)
      const columns = []
      if (res.data.code == 200) {
        res.data.data.forEach(item => {
          columns.push(item.title)
        })
        this.setData({
          columns: columns,
          storeList: res.data.data
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})